//
//  FindScrollView.m
//  quickLook
//
//  Created by J on 15/8/31.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "FindScrollView.h"

#import "Header.h"
#import "ThirdNaviSearchController.h"

@interface FindScrollView () <UIScrollViewDelegate>

{
    
    NSMutableArray* _mutableArray;
    
    CAGradientLayer *_gradientLayer;

    
}

@property (nonatomic, strong) UIScrollView *adScrollView;

@property(nonatomic,strong)NSMutableArray* NEWArray;

@property(nonatomic,strong)NSMutableArray* tapArray;




@property(nonatomic,strong)NSMutableArray* ModelArray;




@end

@implementation FindScrollView
//- (instancetype)initWithFrame:(CGRect)frame andImageArr:(NSMutableArray*)image_url andIdArray:(NSMutableArray*)IDArray;



- (instancetype)initWithFrame:(CGRect)frame andImageArr:(NSMutableArray*)image_url andIdArray:(NSMutableArray*)ModelArray andTitle:(NSString *)title;

{


    if (self=[super initWithFrame:frame]) {
    
        

        _NEWArray=[NSMutableArray array];
        
        
        
        
        [_NEWArray addObjectsFromArray:image_url];
        
        _mutableArray=[NSMutableArray array];
        
        
        
        _adScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, frame.size.height)];

        
        
        

        _adScrollView.showsVerticalScrollIndicator=NO;
        
        _adScrollView.showsHorizontalScrollIndicator=NO;
        
        
        
        
        
        if ([title isEqualToString:@"热门单篇"]) {
            
            
            _adScrollView.contentSize = CGSizeMake(_NEWArray.count*(BIGSCROLLWIDTH+10), frame.size.height);
            
        }else
        {
            _adScrollView.contentSize = CGSizeMake(_NEWArray.count*SCROLLWIDTH+10*_NEWArray.count, frame.size.height);
            
            
        }

        
        
        [self addSubview:_adScrollView];
        _adScrollView.delegate = self;
        

        

        //        通过遍历newImages创建UIImageView对象，并且添加到_adScrollView上面
        [_NEWArray enumerateObjectsUsingBlock:^(NSString *url, NSUInteger idx, BOOL *stop) {
            

            

            if ([title isEqualToString:@"热门单篇"]) {
                
                
                self.ModelArray=ModelArray;
                
                Model* model=ModelArray[idx];
               
                

                
                //             创建imageView对象
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, BIGSCROLLWIDTH, frame.size.height-42)];
                //设置image属性
                
                //整个uiview 用来装ImageView
                
                UIView* view =[[UIView alloc]initWithFrame:CGRectMake(10 +(BIGSCROLLWIDTH+10)*idx,0, BIGSCROLLWIDTH, frame.size.height)];

                view.layer.borderWidth=0.4;
                view.layer.borderColor=[[UIColor lightGrayColor]CGColor];
                
                //imageView下面的babel
                
                UILabel* Titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(5, frame.size.height-40, BIGSCROLLWIDTH,20)];
                Titlelabel.font=[UIFont systemFontOfSize:14];

                Titlelabel.textColor=[UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1.0];
                Titlelabel.text=model.title;
                
                [view addSubview:Titlelabel];
                
                
                //Titlelabel下面的babel
                
                
                UILabel* nicknameLabel=[[UILabel alloc]initWithFrame:CGRectMake(5, frame.size.height-20, BIGSCROLLWIDTH,15)];
                nicknameLabel.font=[UIFont systemFontOfSize:11];
                nicknameLabel.textColor=[UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1.0];

                nicknameLabel.text=model.topic[@"user"][@"nickname"];
                
                [view addSubview:nicknameLabel];
                
                
                
                

;
            
              
                imageView.tag=[model.id integerValue] ;
                
                imageView.tag=idx ;
                
                [view addSubview:imageView];
                
                //            JZJLog(@"XXXXX%zd",imageView.tag);
                //            JZJLog(@"%zd",model.id);
                
                [imageView  sd_setImageWithURL:[NSURL URLWithString:url]];
                
                [_adScrollView addSubview:view];
                
                imageView.userInteractionEnabled = YES;
                
                
                UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hotTap:)];
                [imageView addGestureRecognizer:tap];
                
                
   
                

            }
            else//不属于热门单篇
            {
                
                
                
                
                //             创建imageView对象
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(idx*SCROLLWIDTH+10 + idx*10,0, SCROLLWIDTH, frame.size.height)];
                //            设置image属性
                
                
                
                self.ModelArray=ModelArray;
                
                Model* model=ModelArray[idx];
                imageView.tag=[model.id integerValue] ;
                
                imageView.tag=idx ;
                
                //            JZJLog(@"XXXXX%zd",imageView.tag);
                //            JZJLog(@"%zd",model.id);
                
                [imageView  sd_setImageWithURL:[NSURL URLWithString:url]];
                
                [_adScrollView addSubview:imageView];
                
                imageView.userInteractionEnabled = YES;
                
                
                
                //添加文字
                
                
                
                
                //imageView下面的babel
                
                UILabel* Titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(5, frame.size.height-40, BIGSCROLLWIDTH,30)];
                Titlelabel.font= [UIFont boldSystemFontOfSize:14];
                Titlelabel.textColor=[UIColor whiteColor];
                Titlelabel.text=model.title;

                [imageView addSubview:Titlelabel];
                
                
                //Titlelabel下面的babel
                
                
                UILabel* nicknameLabel=[[UILabel alloc]initWithFrame:CGRectMake(5, frame.size.height-18, BIGSCROLLWIDTH,15)];
                nicknameLabel.font= [UIFont boldSystemFontOfSize:11];
                nicknameLabel.textColor=[UIColor whiteColor];
                nicknameLabel.text=model.user[@"nickname"];
                
                [imageView addSubview:nicknameLabel];

                
                
                
                
                
                _gradientLayer = [CAGradientLayer layer];  // 设置渐变效果
                _gradientLayer.borderWidth = 0;
                
                _gradientLayer.frame = imageView.bounds;
                _gradientLayer.colors = [NSArray arrayWithObjects:
                                         (id)[[UIColor clearColor] CGColor],
                                         (id)[JZJLIGHTGRAY CGColor], nil];
                _gradientLayer.startPoint = CGPointMake(0.5, 0.8);
                _gradientLayer.endPoint = CGPointMake(0.5, 1.0);
                
                [imageView.layer insertSublayer:_gradientLayer atIndex:0];
                
                
                
                
                
                
                
                
                
                
                UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
                [imageView addGestureRecognizer:tap];

                
                
            }
            
//
            
            
        }];
        
        
        
        
        
        
        
    }
    
    
    return self;
}


//如果是热门单篇  需要通过webview打开 不然会崩溃
- (void)hotTap:(UITapGestureRecognizer*)tap

{
    
    Model* model=_ModelArray[tap.view.tag];
    
    
    WebViewController* web=[[WebViewController alloc]init];
    
    
    
    web.model=model;
    web.hidesBottomBarWhenPushed=YES;
    
    [self.delegate.navigationController pushViewController:web animated:YES];
    
    
}

- (void)tap:(UITapGestureRecognizer*)tap
{
    
    

    
    Model* model=_ModelArray[tap.view.tag];
    
    
//    JZJLog(@"%@",model.id);
    
        ThirdNaviSearchController* vc=[[ThirdNaviSearchController alloc]init];

        vc.model=model;
    
        [self.delegate.navigationController pushViewController:vc animated:YES];


    
    
}


#pragma mark --UIScrollViewDelegate




@end
